﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BE;
using System.IO;

namespace DAL
{
	internal class FileAccess
	{
		//The file Access class iterates through each line of the database text file
		//each line is thrown into an AlbumPicker object which will work the albumstring
		//read AlbumPicker for information on how to create filters for lines
		internal static List<Album> readFile(ALbumPicker returnLine)
		{
			try
			{
				List<Album> list = new List<Album>();
				using ( StreamReader reader = new StreamReader( "albums.txt" ) )
				{

					string line;
					while ( ( line = reader.ReadLine() ) != null )
					{
						Album album;
						if(( album= returnLine.workString(line)) != null)
						list.Add(album);
					}
				}
				return list;
			}
			catch ( Exception e )
			{
				
				throw new Exception( "There was an error Parsing the file\ncheck if the file is being used, that you have permission to access it and if it's present in the directory\ntry loading again if you think you fixed the error with the file" );
			}
		}

		internal static Album getAlbum(int id)
		{
			try
			{
				List<Album> list = new List<Album>();
				using ( StreamReader reader = new StreamReader( "albums.txt" ) )
				{

					int current = 0;
					string line;
					do
					{
						line = reader.ReadLine();
						current++;
					} while (current<id );
					return ALbumPicker.AlbumStringToAlbum(ALbumPicker.SplitAlbumString(line));
				}
				
			}
			catch ( Exception e )
			{

				throw new Exception( "There was an error Parsing the file\ncheck if the file is being used, that you have permission to access it and if it's present in the directory\ntry loading again if you think you fixed the error with the file" );
			}
		}
	}
}
